package com.wxzz.elearing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.user.dto.CommonCategoryDto;
import com.wxzz.elearing.user.entity.CommonCategory;
import com.wxzz.elearing.user.mapper.CommonCategoryMapper;
import com.wxzz.elearing.user.service.ICommonCategoryService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 通用分类 服务实现类
 * </p>
 *
 * @author
 * @since 2021-04-01
 */
@Service
public class CommonCategoryServiceImpl extends ServiceImpl<CommonCategoryMapper, CommonCategory> implements ICommonCategoryService {


    @Autowired
    private CommonCategoryMapper commonCategoryMapper;

    @Override
    public ReturnVO<List<CommonCategory>> listCommonCategoryByType(Integer categoryType) {
        if (categoryType == null) {
            throw new ServiceException("请传入分类类型");
        }
        List<CommonCategory> commonCategories = commonCategoryMapper.selectList(new LambdaQueryWrapper<CommonCategory>().eq(CommonCategory::getCategoryType, categoryType));
        return new ReturnVO("分类查询成功", commonCategories);
    }

    @Override
    public ReturnVO<JgPageVo<List<CommonCategory>>> listCommonCategory(CommonCategoryDto commonCategoryDto) {
        PageHelper.startPage(commonCategoryDto.getCurPage(), commonCategoryDto.getMaxPage());
        QueryWrapper<CommonCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_type", commonCategoryDto.getCategoryType());
        if (commonCategoryDto.getEnterpriseId() != null) {
            queryWrapper.eq("enterprise_id", commonCategoryDto.getEnterpriseId());
        }
        if(StringUtils.isNotBlank(commonCategoryDto.getCategoryName())){
            queryWrapper.like("category_name", commonCategoryDto.getCategoryName());
        }
        List<CommonCategory> commonCategories = commonCategoryMapper.selectList(queryWrapper);
        return new ReturnVO("分类查询成功", new JgPageVo<>(new PageInfo(commonCategories)));
    }

    @Override
    public ReturnVO saveCommonCategoryByType(CommonCategory commonCategory) {
        if (commonCategory.getId() == null) {
            commonCategoryMapper.insert(commonCategory);
        } else {
            commonCategoryMapper.updateById(commonCategory);
        }
        return new ReturnVO("分类新增成功", commonCategory.getId());
    }

    @Override
    public ReturnVO deleteCommonCategoryById(Long commonCategoryId) {
        commonCategoryMapper.deleteById(commonCategoryId);
        return new ReturnVO("分类删除成功",commonCategoryId);
    }
}
